package com.lagou.stage1.module3.task3;

import java.util.*;

public class StudentTest {
    public static void main(String[] args) {

        //(2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
        Student s1 = new Student("孙悟空",33);
        Student s2 = new Student("猪八戒",32);
        Student s3 = new Student("沙悟净",31);
        //定义Map对象
        Map<Object,String> m1 =  new HashMap<>();
        //使用put方法放入key value值
        m1.put(s1,"花果山水帘洞");
        m1.put(s2,"高老庄");
        m1.put(s3,"流沙河");


        //(3)利用四种方式遍历Map集合中的内容，格式：key::value
        //方式1: 使用Set keySet()获取
        Set<Object> st1 = m1.keySet();
        for (Object student : st1) {
            System.out.println(student + "::" + m1.get(student));
        }
        System.out.println("-------------------------------------------------------");
        //方式2: Collection values()获取
        Collection<String> co = m1.values();
        for (String addr : co) {
            System.out.println("::" + addr);
        }
        System.out.println("-------------------------------------------------------");
        //方式3: Set<Map.Entry<K,V>> entrySet()
        Set<Map.Entry<Object, String>> entries = m1.entrySet();
        for (Map.Entry<Object, String> me: entries) {
            System.out.println(me.getKey() + "::" + me.getValue());
        }
        System.out.println("-------------------------------------------------------");
        //方式4：使用迭代器
        Iterator<Map.Entry<Object, String>> it = m1.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Object, String> next = it.next();
            System.out.println(next.getKey() + "::" + next.getValue());
        }
    }
}
